﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoDbObject;
using System.Xml;
using SZJT.Utility.DataSource;

namespace Colligate.DataSource.DriverTraining.Maintain
{
    class DataNode_MaintainSchedule : BaseDataTree
    {
        private const string DATATYPE_SCHEDULE_INFO = "MtanScheduleInfo";
        private const string DATATYPE_SCHEDULE_QUERY = "MtanScheduleInfoQuery";
        private const string DATATYPE_SCHEDULE_BATCH_Set = "MtanScheduleBatchSet";


        public DataNode_MaintainSchedule(string taskGuid, IDataSourceLayer dataLayer)
            : base(taskGuid, dataLayer)
        {
            _dataTypeList[DataTypeCategory.GET_DATA_LIST] = new string[] { DATATYPE_SCHEDULE_INFO };
            _dataTypeList[DataTypeCategory.SET_DATA_LIST] = new string[] { DATATYPE_SCHEDULE_INFO, DATATYPE_SCHEDULE_BATCH_Set };
            _dataTypeList[DataTypeCategory.TRANSFORM_LIST] = new string[] { DATATYPE_SCHEDULE_QUERY };
            _dataDefine = new MtanScheduleInfo(dataLayer);

        }

        #region Public Method

        public override int SetData(OUDataInfo UserInfo, string DataGuid, string DataType, System.Xml.XmlNode xmlData)
        {
            int nResult = 0;
            switch (DataType)
            {
                case DATATYPE_SCHEDULE_INFO:
                    nResult = SetScheduleInfo(UserInfo, DataGuid, xmlData);
                    break;
                case DATATYPE_SCHEDULE_BATCH_Set:
                    nResult = SetBatchScheduleInfo(UserInfo, DataGuid, xmlData);
                    break;


            }

            return nResult;
        }


        public override XmlNode GetData(OUDataInfo UserInfo, string DataGuid, string DataType)
        {
            XmlNode ResultInfo = null;
            switch (DataType)
            {
                case DATATYPE_SCHEDULE_INFO:
                    ResultInfo = GetScheduleInfo(UserInfo, DataGuid);
                    break;

            }

            return ResultInfo;
        }


        public override XmlNode TransformData(OUDataInfo UserInfo, string DataType, XmlNode XmlParam)
        {
            XmlNode ResultInfo = null;
            switch (DataType)
            {
                case DATATYPE_SCHEDULE_QUERY:
                    ResultInfo = GetScheduleList(UserInfo, XmlParam);
                    break;
            }

            return ResultInfo;
        }

        #endregion


        #region protected Method
        //应用标识：智慧驾培学事管理系统
        //模块分类：基础信息 
        //功能分类：作息时间表 
        //分类序号：1001 
        //数据编号：ZHJP-MAINTAIN-SCHEDULE-1001 
        // 数据类型：元数据 
        // 数据名称：驾校作息时间信息 
        // 数据代码：MtanSchoolScheduleInfo
        // 数据说明：定义驾校的放假日期，工作日的培训开放时间
        protected XmlNode GetScheduleInfo(OUDataInfo userInfo, string scheduleGuid)
        {
            //if (!CheckPrivilege(userInfo, "操作成员"))
            //    return null;

            IDBDataObject InfoData = (_dataDefine as MtanScheduleInfo).GetScheduleInfoData(scheduleGuid);

            XmlNode xmlNotesInfoData = XmlNodeManage.GetDocumentNode(_taskGuid, scheduleGuid, DATATYPE_SCHEDULE_INFO);


            XmlNodeManage.SetSubValue(xmlNotesInfoData, "ScheduleGuid", "GUID", InfoData["fScheduleGuid"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "ScheduleDate", "DATE", InfoData["fScheduleDate"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "IsWorkingDay", "TEXT", InfoData["fIsWorkingDay"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "MorningStart", "SINGLE", InfoData["fMorningStart"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "MorningHours", "LONG", InfoData["fMorningHours"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "AfternoonStart", "SINGLE", InfoData["fAfternoonStart"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "AfternoonHours", "LONG", InfoData["fAfternoonHours"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "EveningStart", "SINGLE", InfoData["fEveningStart"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "EveningHours", "LONG", InfoData["fEveningHours"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "ScheduleMemo", "TEXT", InfoData["fScheduleMemo"]);

            return xmlNotesInfoData;
        }


        protected XmlNode GetScheduleList(OUDataInfo userInfo, XmlNode xmlParam)
        {
            string queryScheduleDateStart = XmlNodeManage.GetSubValue(xmlParam, "ScheduleStartDate");
            string queryScheduleDateEnd = XmlNodeManage.GetSubValue(xmlParam, "ScheduleEndDate");
            //string queryIsWorkingDay = XmlNodeManage.GetSubValue(xmlParam, "IsWorkingDay");
            //string queryMorningStart = XmlNodeManage.GetSubValue(xmlParam, "MorningStart");
            //string queryMorningHours = XmlNodeManage.GetSubValue(xmlParam, "MorningHours");
            //string queryAfternoonStart = XmlNodeManage.GetSubValue(xmlParam, "AfternoonStart");
            //string queryAfternoonHours = XmlNodeManage.GetSubValue(xmlParam, "AfternoonHours");
            //string queryEveningStart = XmlNodeManage.GetSubValue(xmlParam, "EveningStart");
            //string queryEveningHours = XmlNodeManage.GetSubValue(xmlParam, "EveningHours");
            //string queryScheduleMemo = XmlNodeManage.GetSubValue(xmlParam, "ScheduleMemo");
            string queryPageIndex = XmlNodeManage.GetSubValue(xmlParam, "PageIndex");
            string queryPageSize = XmlNodeManage.GetSubValue(xmlParam, "PageSize");

            IDBBigObjectSet schedules = (_dataDefine as MtanScheduleInfo).GetScheduleByDate(queryScheduleDateStart, queryScheduleDateEnd);

            Pagination dataPage = new Pagination(schedules);
            List<IDBDataObject> scheduleList = dataPage.GetPageDataSet(queryPageIndex, queryPageSize);
            queryPageIndex = dataPage.PageIndex.ToString();
            queryPageSize = dataPage.PageSize.ToString();

            XmlNode xmlQueryData = XmlNodeManage.GetDocumentNode(_taskGuid, "", DATATYPE_SCHEDULE_QUERY);

            //Add the paging nodes.
            XmlNodeManage.SetSubValue(xmlQueryData, "RecordCount", "LONG", schedules.DataCount);
            XmlNodeManage.SetSubValue(xmlQueryData, "PageSize", "LONG", queryPageSize);
            XmlNodeManage.SetSubValue(xmlQueryData, "PageIndex", "LONG", queryPageIndex);

            XmlNode scheduleRoot = XmlNodeManage.CreateNode("ScheduleList");
            XmlNode subNode;

            foreach (IDBDataObject schedule in scheduleList)
            {
                subNode = XmlNodeManage.CreateNode("ScheduleInfo");

                XmlNodeManage.SetSubValue(subNode, "ScheduleGuid", "GUID", schedule["fScheduleGuid"]);
                XmlNodeManage.SetSubValue(subNode, "ScheduleDate", "DATE", schedule["fScheduleDate"]);
                XmlNodeManage.SetSubValue(subNode, "IsWorkingDay", "TEXT", schedule["fIsWorkingDay"]);
                XmlNodeManage.SetSubValue(subNode, "MorningStart", "SINGLE", schedule["fMorningStart"]);
                XmlNodeManage.SetSubValue(subNode, "MorningHours", "LONG", schedule["fMorningHours"]);
                XmlNodeManage.SetSubValue(subNode, "AfternoonStart", "SINGLE", schedule["fAfternoonStart"]);
                XmlNodeManage.SetSubValue(subNode, "AfternoonHours", "LONG", schedule["fAfternoonHours"]);
                XmlNodeManage.SetSubValue(subNode, "EveningStart", "SINGLE", schedule["fEveningStart"]);
                XmlNodeManage.SetSubValue(subNode, "EveningHours", "LONG", schedule["fEveningHours"]);
                XmlNodeManage.SetSubValue(subNode, "ScheduleMemo", "TEXT", schedule["fScheduleMemo"]);

                scheduleRoot.AppendChild(subNode);
            }

            xmlQueryData.AppendChild(scheduleRoot);

            return xmlQueryData;
        }


        //驾校作息时间信息
        // <!-- 应用标识：智慧驾培学事管理系统 -->
        // <!-- 模块分类：基础信息 -->
        // <!-- 功能分类：作息时间表 -->
        // <!-- 分类序号：1001 -->
        // <!-- 数据编号：ZHJP-MAINTAIN-SCHEDULE-1001 -->
        // <!-- 数据类型：元数据 -->
        // <!-- 数据名称：驾校作息时间信息 -->
        // <!-- 数据代码：MtanSchoolScheduleInfo -->
        // <!-- 数据说明：定义驾校的放假日期，工作日的培训开放时间 -->
        protected int SetScheduleInfo(OUDataInfo userInfo, string scheduleGuid, System.Xml.XmlNode xmlData)
        {
            //if (!CheckPrivilege(userInfo, "操作成员"))
            //    return null;

            IDBDataObject InfoData = (_dataDefine as MtanScheduleInfo).GetScheduleInfoData(scheduleGuid);

            int result = 0;
            InfoData["fScheduleGuid"] = XmlNodeManage.GetSubValue(xmlData, "ScheduleGuid");
            InfoData["fScheduleDate"] = XmlNodeManage.GetSubValue(xmlData, "ScheduleDate");
            InfoData["fIsWorkingDay"] = XmlNodeManage.GetSubValue(xmlData, "IsWorkingDay");
            InfoData["fMorningStart"] = XmlNodeManage.GetSubValue(xmlData, "MorningStart");
            InfoData["fMorningHours"] = XmlNodeManage.GetSubValue(xmlData, "MorningHours");
            InfoData["fAfternoonStart"] = XmlNodeManage.GetSubValue(xmlData, "AfternoonStart");
            InfoData["fAfternoonHours"] = XmlNodeManage.GetSubValue(xmlData, "AfternoonHours");
            InfoData["fEveningStart"] = XmlNodeManage.GetSubValue(xmlData, "EveningStart");
            InfoData["fEveningHours"] = XmlNodeManage.GetSubValue(xmlData, "EveningHours");
            InfoData["fScheduleMemo"] = XmlNodeManage.GetSubValue(xmlData, "ScheduleMemo");

            result = InfoData.SaveData() ? 0 : 1;
            return result;
        }


        //批量设置驾校作息时间信息
        //<!-- 应用标识：智慧驾培学事管理系统 -->
        //<!-- 模块分类：基础信息 -->
        //<!-- 功能分类：作息时间表 -->
        //<!-- 分类序号：1002 -->
        //<!-- 数据编号：ZHJP-MAINTAIN-SCHEDULE-1002 -->
        //<!-- 数据类型：复合数据 -->
        //<!-- 数据名称：批量设置驾校作息时间信息 -->
        //<!-- 数据代码：MtanSchoolBatchSchedule -->
        //<!-- 数据说明：批量定义驾校的放假日期，工作日的培训开放时间，只支持Set -->
        protected int SetBatchScheduleInfo(OUDataInfo userInfo, string batchGuid, XmlNode xmlData)
        {
            //if (!CheckPrivilege(userInfo, "操作成员"))
            //    return null;

            string scheduleStartDate = XmlNodeManage.GetSubValue(xmlData, "ScheduleStartDate");
            string scheduleEndDate = XmlNodeManage.GetSubValue(xmlData, "ScheduleEndDate");

            bool isSipWeekend = XmlNodeManage.GetSubValue(xmlData, "IsSkipWeekend") == "T" ? true : false;

            int result = 0;
            DateTime start = DateTime.Parse(scheduleStartDate);
            DateTime end = DateTime.Parse(scheduleEndDate);

            while (start <= end)
            {
                
                string scheduleGuid = System.Guid.NewGuid().ToString();
               
                if (isSipWeekend && start.DayOfWeek != DayOfWeek.Saturday && start.DayOfWeek != DayOfWeek.Sunday)
                {
                    IDBDataObject InfoData = (_dataDefine as MtanScheduleInfo).GetScheduleInfoData(scheduleGuid);
                    InfoData["fScheduleGuid"] = scheduleGuid;
                    InfoData["fScheduleDate"] = start.ToString();
                    InfoData["fIsWorkingDay"] = XmlNodeManage.GetSubValue(xmlData, "IsWorkingDay");
                    InfoData["fMorningStart"] = XmlNodeManage.GetSubValue(xmlData, "MorningStart");
                    InfoData["fMorningHours"] = XmlNodeManage.GetSubValue(xmlData, "MorningHours");
                    InfoData["fAfternoonStart"] = XmlNodeManage.GetSubValue(xmlData, "AfternoonStart");
                    InfoData["fAfternoonHours"] = XmlNodeManage.GetSubValue(xmlData, "AfternoonHours");
                    InfoData["fEveningStart"] = XmlNodeManage.GetSubValue(xmlData, "EveningStart");
                    InfoData["fEveningHours"] = XmlNodeManage.GetSubValue(xmlData, "EveningHours");
                    InfoData["fScheduleMemo"] = XmlNodeManage.GetSubValue(xmlData, "ScheduleMemo");
                    result = InfoData.SaveData() ? 0 : 1;
                    if (result == 1) break;
                }
                start = start.AddDays(1);
              
            }
            return result;
        }

        #endregion
    }
}
